package com.cxlin.cwanandroid.ui.systemtype

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.cxlin.base.BaseActivity
import com.cxlin.base.ext.binding
import com.cxlin.base.ext.extraActivity
import com.cxlin.base.ext.newInstanceFragment
import com.cxlin.cwanandroid.R
import com.cxlin.cwanandroid.databinding.ActivitySystemTypeNormalBinding
import com.cxlin.cwanandroid.model.bean.SystemParent
import com.cxlin.cwanandroid.ui.systemtype.fragment.SystemTypeFragment
import com.google.android.material.tabs.TabLayoutMediator

/**
 *
 * @Author: chenxunlin
 * @Date: 2024/4/30 16:49
 */
class SystemTypeNormalActivity : BaseActivity() {

    private val binding by binding<ActivitySystemTypeNormalBinding>(R.layout.activity_system_type_normal)
    private val systemParent by extraActivity<SystemParent>("info")

    override fun initView(savedInstanceState: Bundle?) {
        binding.mToolbar.run {
            title = systemParent?.name?:""
            setNavigationIcon(R.drawable.arrow_back)
            setNavigationOnClickListener {
                onBackPressed()
            }
        }
        initViewPager()
    }


    private fun initViewPager(){
        systemParent?.run {
            binding.viewPager.adapter = object : FragmentStateAdapter(this@SystemTypeNormalActivity){
                override fun getItemCount(): Int = children.size

                override fun createFragment(position: Int): Fragment =
                    this@SystemTypeNormalActivity.newInstanceFragment<SystemTypeFragment>(
                        Bundle().apply {
                            putSerializable("cid",children[position].id) }
                    )

            }
            TabLayoutMediator(binding.tabLayout,binding.viewPager){tab,position->
                tab.text = children[position].name
            }.attach()
        }
    }

    override fun initData(savedInstanceState: Bundle?) {
    }

    override fun observeStateAndEvent() {
    }
}